package com.tian.service.impl;

import com.tian.common.CommonResult;
import com.tian.dto.OrderUseCouponDto;
import com.tian.dto.UserBalanceRespDto;
import com.tian.entity.OrderUseCoupon;
import com.tian.entity.UserBalance;
import com.tian.mapper.OrderUseCouponMapper;
import com.tian.mapper.UserBalanceMapper;
import com.tian.service.QueryPayOrderService;
import com.tian.util.UserCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * {@code @description:} 支付订单查询业务实现类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2025-01-20 10:16
 * {@code @version:} 1.0
 */
@Slf4j
@Service
public class QueryPayOrderServiceImpl implements QueryPayOrderService {
    @Resource
    private UserBalanceMapper userBalanceMapper;
    @Resource
    private OrderUseCouponMapper orderUseCouponMapper;

    @Override
    public CommonResult<List<OrderUseCouponDto>> queryOrderUseCoupon(Long orderId) {
        List<OrderUseCoupon> orderUseCouponList = orderUseCouponMapper.selectByOrderId(orderId);
        if (!CollectionUtils.isEmpty(orderUseCouponList)) {
            List<OrderUseCouponDto> orderUseCouponDtoList = new ArrayList<>();
            orderUseCouponList.forEach(orderUseCoupon -> {
                OrderUseCouponDto orderUseCouponDto = new OrderUseCouponDto();
                orderUseCouponDto.setId(orderUseCoupon.getId());
                orderUseCouponDto.setOrderId(orderUseCoupon.getOrderId());
                orderUseCouponDto.setUserCouponId(orderUseCoupon.getUserCouponId());
                orderUseCouponDto.setStatus(orderUseCoupon.getStatus());
                orderUseCouponDtoList.add(orderUseCouponDto);
            });
            return CommonResult.success(orderUseCouponDtoList);
        }
        log.error("订单:{}未使用优惠券", orderId);
        return CommonResult.failed("订单未使用优惠券");
    }

    @Override
    public CommonResult<UserBalanceRespDto> queryBalance() {
        Long userId = UserCacheUtil.getUser().getId();
        try {
            UserBalance userBalance = userBalanceMapper.selectByUserId(userId);
            if (userBalance == null) {
                userBalance = new UserBalance();
                userBalance.setFreezeBalance(BigDecimal.ZERO);
                userBalance.setUserId(userId);
                userBalance.setBalance(BigDecimal.ZERO);
                userBalance.setCreateTime(new Date());
                userBalanceMapper.insert(userBalance);
            }
            UserBalanceRespDto userBalanceRespDto = new UserBalanceRespDto();
            if (userBalance.getFreezeBalance() == null) {
                userBalance.setFreezeBalance(BigDecimal.ZERO);
            }
            BigDecimal subtract = userBalance.getBalance().subtract(userBalance.getFreezeBalance());
            subtract = subtract.setScale(2, RoundingMode.HALF_UP);
            userBalanceRespDto.setBalance(subtract.toString());
            return CommonResult.success(userBalanceRespDto);
        } catch (Exception e) {
            log.error("用户:{}查询余额失败", userId, e);
        }
        return CommonResult.failed("用户余额查询失败");
    }
}
